<template>
  <div class="update-birthday">
    <van-datetime-picker
      v-model="currentDate"
      type="date"
      title="生日"
      :min-date="minDate"
      :max-date="maxDate"
      @cancel="$emit('close')"
      @confirm='onConfirm'
    />
  </div>
</template>

<script>
import { updateUserProfile } from '@/apis/user'
import dayjs from 'dayjs'
export default {
  name: 'UpdateBirthday',
  props: {
    birthday: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      minDate: new Date(2000, 4, 20),
      maxDate: new Date(),
      currentDate: new Date(this.birthday)
    }
  },
  methods: {
    async onConfirm () {
      this.$toast.loading({
        message: '保存中',
        forbidClick: true
      })
      await updateUserProfile({
        birthday: dayjs(this.currentDate).format('YYYY-MM-DD')
      })
      this.$toast.success('保存成功')
      this.$emit('update:birthday', dayjs(this.currentDate).format('YYYY-MM-DD'))
      this.$emit('close')
    }
  }
}
</script>

<style lang="less" scoped>
.field-wrap{
  padding: 10px;
}
</style>
